<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>商品管理</el-breadcrumb-item>
			<el-breadcrumb-item>商品分类</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 卡片区域 -->
		<el-card>
			<!-- 添加分类按钮 -->
			<el-row>
				<el-col>
					<el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
				</el-col>
			</el-row>
			<!-- 表格区域 -->
			<tree-table
				class="treeTable"
				:data="catelist"
				:columns="columns"
				:expand-type="false"
				:selection-type="false"
				show-index
				index-text="#"
				border
				:show-row-hover="false"
			>
				<!-- 是否有效 -->
				<template v-slot:isok="scope">
					<i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color:green;"></i>
					<i class="el-icon-error" v-if="scope.row.cat_deleted" style="color:red;"></i>
				</template>
				<!-- 排序 -->
				<template v-slot:order="scope">
					<el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
					<el-tag type="success" size="mini" v-if="scope.row.cat_level === 1">二级</el-tag>
					<el-tag type="warning" size="mini" v-if="scope.row.cat_level === 2">三级</el-tag>
				</template>
				<!-- 操作 -->
				<template v-slot:opt="scope">
					<el-button
						type="primary"
						icon="el-icon-edit"
						size="mini"
						@click="showEditCateDialog(scope.row)"
					>编辑</el-button>
					<el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteBtn(scope.row)">删除</el-button>
				</template>
			</tree-table>
			<!-- 分页区域 -->
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="querInfo.pagenum"
				:page-sizes="[3, 5, 10, 15]"
				:page-size="querInfo.pagesize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>

			<!-- 对话框 -->
			<!-- 添加对话对话框 -->
			<el-dialog
				title="添加分类"
				:visible.sync="addCateDialogVisible"
				width="50%"
				@close="addCateDialogClosed"
			>
				<el-form
					:model="addCateForm"
					:rules="addCateFormRules"
					ref="addCateFormRef"
					label-width="100px"
				>
					<el-form-item label="分类名称" prop="cat_name">
						<el-input v-model="addCateForm.cat_name"></el-input>
					</el-form-item>
					<el-form-item label="父级名称">
						<el-cascader
							v-model="selectedKeys"
							:options="parentCateList"
							:props="cascaderProps"
							@change="parentCateChanged"
							clearable
						></el-cascader>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="addCateDialogVisible = false">取 消</el-button>
					<el-button type="primary" @click="addCate">确 定</el-button>
				</div>
			</el-dialog>
			<!-- 编辑和删除 -->

			<!-- 编辑视频分类对话框 -->
			<el-dialog
				title="编辑分类"
				:visible.sync="editCateDialogVisible"
				width="50%"
				@close="editCateDialogClosed"
			>
				<el-form
					:model="editCateForm"
					:rules="editCateFormRules"
					ref="editCateFormRef"
					label-width="100px"
				>
					<el-form-item label="分类名称" prop="cat_name">
						<el-input v-model="editCateForm.cat_name"></el-input>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="editCateDialogVisible = false">取 消</el-button>
					<el-button type="primary" @click="editCate">确 定</el-button>
				</div>
			</el-dialog>
			<!-- 编辑和删除 -->
		</el-card>
	</div>
</template>
<script>
export default {
	data() {
		return {
			//查询条件
			querInfo: {
				type: 3,
				pagenum: 1,
				pagesize: 5
			},
			//商品分类数据列表
			catelist: [],
			//总数据条数
			total: 0,
			//tree-table属性
			columns: [
				{
					label: '分类名称',
					prop: 'cat_name'
				},
				{
					label: '是否有效',
					type: 'template',
					template: 'isok'
				},
				{
					label: '排序',
					type: 'template',
					template: 'order'
				},
				{
					label: '操作',
					type: 'template',
					template: 'opt'
				}
			],
			addCateDialogVisible: false,
			addCateForm: {
				cat_name: '',
				cat_pid: 0,
				cat_level: 0
			},
			addCateFormRules: {
				cat_name: [
					{
						required: true,
						message: '请输入活动名称',
						trigger: 'blur'
					},
					{
						min: 1,
						max: 15,
						message: '长度在 1 到 15 个字符',
						trigger: 'blur'
					}
				]
			},
			parentCateList: [],
			cascaderProps: {
				value: 'cat_id',
				label: 'cat_name',
				children: 'children',
				expandTrigger: 'hover',
				checkStrictly: 'true'
			},
			selectedKeys: [],
			editCateDialogVisible: false,
			editCateForm: {
				cat_name: '',
				cat_id: ''
			},
			editCateFormRules: {
				cat_name: [
					{
						required: true,
						message: '请输入活动名称',
						trigger: 'blur'
					},
					{
						min: 1,
						max: 15,
						message: '长度在 1 到 15 个字符',
						trigger: 'blur'
					}
				]
			}
		}
	},
	created() {
		this.getCateList()
	},
	methods: {
		async getCateList() {
			//获取商品分类的数据列表
			const { data: res } = await this.$http.get('categories', {
				params: this.querInfo
			})
			if (res.meta.status !== 200)
				return this.$message.error({
					message: res.meta.msg,
					offset: 100
				})
			this.catelist = res.data.result
			this.total = res.data.total
		},
		handleSizeChange(newSize) {
			//监听pagesize改变
			this.querInfo.pagesize = newSize
			this.getCateList()
		},
		handleCurrentChange(newPage) {
			//监听当前pagenum的改变
			this.querInfo.pagenum = newPage
			this.getCateList()
		},
		showAddCateDialog() {
			//打开添加按钮对话框
			this.getParentCateList()
			this.addCateDialogVisible = true
		},
		async getParentCateList() {
			//获取父级分类的数据列表
			const { data: res } = await this.$http.get('categories', {
				params: { type: 2 }
			})
			if (res.meta.status !== 200)
				return this.$message.error({
					message: res.meta.msg,
					offset: 100
				})
			this.parentCateList = res.data
		},
		parentCateChanged() {
			//选择项变化时触发
			if (this.selectedKeys.length > 0) {
				this.addCateForm.cat_pid = this.selectedKeys[
					this.selectedKeys.length - 1
				]
				this.addCateForm.cat_level = this.selectedKeys.length
				return
			} else {
				this.addCateForm.cat_pid = 0
				this.addCateForm.cat_level = 0
			}
		},
		addCate() {
			//添加商品对话框的确定按钮
			this.$refs.addCateFormRef.validate(async valid => {
				if (!valid) return
				const { data: res } = await this.$http.post(
					'categories',
					this.addCateForm
				)
				if (res.meta.status !== 201)
					return this.$message.error({
						message: res.meta.msg,
						offset: 100
					})
				this.$message.success({
					message: res.meta.msg,
					offset: 100
				})
				this.getCateList()
				this.addCateDialogVisible = false
			})
		},
		addCateDialogClosed() {
			this.$refs.addCateFormRef.resetFields()
			this.selectedKeys = []
			this.addCateForm.cat_pid = 0
			this.addCateForm.cat_level = 0
		},
		showEditCateDialog(arg) {
			//点击编辑打开对话框

			this.editCateForm.cat_name = arg.cat_name
			this.editCateForm.cat_id = arg.cat_id
			this.editCateDialogVisible = true
		},
		editCateDialogClosed() {
			//编辑分类对话框关闭
			this.$refs.editCateFormRef.resetFields()
		},
		editCate() {
			//编辑分类对话框的确定按钮
			this.$refs.editCateFormRef.validate(async valid => {
				if (!valid) {
					alert('请正确填写信息')
					return false
				}
				const { data: res } = await this.$http.put(
					'categories/' + this.editCateForm.cat_id,
					{ cat_name: this.editCateForm.cat_name }
				)
				if (res.meta.status !== 200)
					return this.$message.error({
						message: res.meta.msg,
						offset: 100
					})
				this.$message.success({
					message: res.meta.msg,
					offset: 100
				})
				this.getCateList()
			})

			this.editCateDialogVisible = false
		},
		deleteBtn(arg) {
			//删除按钮
			this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					this.$http
						.delete('categories/' + arg.cat_id)
						.then(res => {
							if (res.data.meta.status !== 200)
								return this.$message.error({
									message: res.data.meta.msg,
									offset: 100
								})
							this.$message({
								type: 'success',
								message: res.data.meta.msg,
								offset: 100
							})
							this.getCateList()
						})
						.catch(err => err)
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除',
						offset: 100
					})
				})
		}
	}
}
</script>
<style lang="less" scoped>
.treeTable {
	margin-top: 15px;
}
.el-cascader {
	width: 100%;
}
</style>